-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add support for durable execution with Prefect #3074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for durable execution with Prefect #3074
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a couple small thoughts
pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_function_toolset.py
Outdated
Show resolved
Hide resolved
pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_function_toolset.py
Outdated
Show resolved
Hide resolved
pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_function_toolset.py
Outdated
Show resolved
Hide resolved
pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_mcp_server.py
Outdated
Show resolved
Hide resolved
tests/test_prefect.py
Outdated
pytest.mark.anyio, | ||
pytest.mark.vcr, | ||
pytest.mark.xdist_group(name='prefect'), | ||
pytest.mark.filterwarnings('ignore:Found propagated trace context.*:RuntimeWarning'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefect populates a traceparent
header, so Logfire is raising a warning. Looks like I can handle that by providing a value to distributed_tracing
, but I might need to make a modification to the capfire
fixture to allow that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realize the capfire
fixture is from logfire
. I think I'll need to filter that warning in one test.
Thanks for the review @DouweM! This PR is ready for another review when you get the chance! |
docs/durable_execution/prefect.md
Outdated
* Wraps [tool calls](../tools.md) as Prefect tasks (configurable per-tool). | ||
* Wraps [MCP communication](../mcp/client.md) as Prefect tasks. | ||
|
||
Event stream handlers are **not automatically wrapped** by Prefect. If they involve I/O or non-deterministic behavior, you can explicitly decorate them with `@task` from Prefect. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we link to the section in agents.md that introduces event stream handlers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a link in a941da4 to what I think is the right page and section, but please correct me if I'm wrong.
* **Prefect**: Workflow-level orchestration, task status, and retry history | ||
* **Logfire**: Fine-grained tracing of agent runs, model requests, and tool invocations | ||
|
||
When using Logfire with Prefect, you can enable distributed tracing to see spans for your Prefect runs included with your agent runs, model requests, and tool invocations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does Prefect need any special setup to sent OTel data to Logfire?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope! As long as Logfire is configured, it'll send OTel data automatically.
from pydantic_ai.durable_exec.prefect._cache_policies import DEFAULT_PYDANTIC_AI_CACHE_POLICY | ||
|
||
|
||
class TaskConfig(TypedDict, total=False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no such type in Prefect? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, there is not. We have an open issue to expose something like this (PrefectHQ/prefect#14851), which provides good motivation to implement that type natively. I'll try to get this into our next release and update this PR, or create a new one if this one has already been merged.
Summary
This PR introduces an integration with Prefect to enable durable, fault-tolerant execution of Pydantic AI agents.
Examples
Run a
pydantic-ai
agent as a Prefect flowThe
PrefectAgent
class wraps anAgent
and instruments it so that.run
calls are executed as a Prefect flow, and all tool and model calls are executed as Prefect tasks.Customize underlying task behavior
Create an agent service to enable scheduling and remote execution
Implementation Details
The integration is implemented through: